<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Finally</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>Finally <span class="ver">[v1.1.14+]</span></h1>

<p>Ensures that one or more statements (commands or expressions) are always executed after the execution of a <a href="Try.htm">try</a> statement.</p>

<pre class="Syntax">Finally <i>Statement</i></pre>
<pre class="Syntax" style="line-height: 100%">Finally
{
    <i>Statements</i>
}</pre>
<h3>Remarks</h3>
<p>Every use of <em>finally</em> must belong to (be associated with) a <a href="Try.htm">try</a> (or <a href="Catch.htm">catch</a>) statement above it. A <em>finally</em> always belongs to the nearest unclaimed <em>try</em> statement above it unless a <a href="Block.htm">block</a> is used to change that behavior.</p>
<p><em>Try</em> statements behave differently depending on whether <em>catch</em> or <em>finally</em> is present. For more information, see <a href="Try.htm">Try</a>.</p>
<p><em>Goto</em>, <em>break</em>, <em>continue</em> and <em>return</em> cannot be used to exit a <em>finally</em> block, as that would require suppressing any control flow instructions within the <em>try</em> block. For example, if <em>try</em> uses <code>return 42</code>, the value 42 is returned after the finally block executes. Attempts to jump out of a <em>finally</em> block using one of these commands are detected as errors at load time where possible, or at run time otherwise.</p>
<p>Prior to v1.1.19.02, a bug existed which prevented control flow statements within <em>try</em> from working when <em>finally</em> was present. <em>Return</em> was erroneously permitted within <em>finally</em>, but was ignored if an exception had been thrown.</p>
<p>The <a href="Block.htm#otb">One True Brace (OTB) style</a> may optionally be used with the <em>finally</em> command. For example:</p>
<pre>try <strong>{</strong>
    ...
<strong>}</strong> finally <strong>{</strong>
    ...
<strong>}</strong>

try <strong>{</strong>
    ...
<strong>}</strong> catch e <strong>{</strong>
    ...
<strong>}</strong> finally <strong>{</strong>
    ...
<strong>}</strong></pre>
<h3>Related</h3>
<p><a href="Try.htm">Try</a>, <a href="Catch.htm">Catch</a>, <a href="Throw.htm">Throw</a>, <a href="Block.htm">Blocks</a></p>
<h3 id="Examples">Examples</h3>
<pre class="NoIndent">try
{
    ToolTip, Working...
    Example1()
}
catch e
{
    <em>; For more detail about the object that e contains, see <a href="Catch.htm">Catch</a>.</em>
    MsgBox, 16,, % &quot;Exception thrown!`n`nwhat: &quot; e.what &quot;`nfile: &quot; e.file
        . &quot;`nline: &quot; e.line &quot;`nmessage: &quot; e.message &quot;`nextra: &quot; e.extra
}
finally
{
    ToolTip <em>; hide the ToolTip</em>
}

MsgBox, Done!

<em>; This function has a Finally block that acts as cleanup code</em>
Example1()
{
    try
        Example2()
    finally
        MsgBox, This is always executed regardless of exceptions
}

<em>; This function fails when the minutes are odd</em>
Example2()
{
    if Mod(A_Min, 2)
        throw Exception(&quot;Test exception&quot;)
    MsgBox, Example2 did not fail
}</pre>

</body>
</html>
